package xyz.syyrjx.initializer;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/9 - 15:01
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class SyyrjxRpcChannelInitializer extends ChannelInitializer<SocketChannel> {

    private ChannelHandler handler;

    private int maxReadSize;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline()
                .addLast(new LineBasedFrameDecoder(maxReadSize))
                .addLast(new StringDecoder(StandardCharsets.UTF_8))
                .addLast(new StringEncoder(StandardCharsets.UTF_8))
                .addLast(handler);
    }
}
